package com.rafahead.bloquinho.dao;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.rafahead.bloquinho.model.Match;
import com.rafahead.bloquinho.util.BasicoDAO;
import com.rafahead.bloquinho.util.TableBuilder;
import com.rafahead.bloquinho.util.Utils;

public class MatchDAO extends BasicoDAO {

	public MatchDAO(Context ctx) {
		super(ctx);
	}

	public static final String TABLE_NAME = "MATCH";

	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_OPEN = "OPEN";
	public static final String COLUMN_GOAL = "GOAL";
	public static final String COLUMN_TOTAL = "TOTAL";
	public static final String[] COLUMNS = {COLUMN_ID};

	private static String defineTable() {
		TableBuilder tb = new TableBuilder(TABLE_NAME);
		try {
			tb.setPrimaryKey(COLUMN_ID, TableBuilder.INTEGER, true);
			tb.addColuna(COLUMN_OPEN, TableBuilder.INTEGER, true);
			tb.addColuna(COLUMN_GOAL, TableBuilder.INTEGER, true);
			tb.addColuna(COLUMN_TOTAL, TableBuilder.INTEGER, true);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return tb.toString();
	}

	/**
	 * 
	 */
	public static final String TABLE_CREATE = defineTable();

	public static void createDummyData(SQLiteDatabase db) {

		/*		ContentValues values = new ContentValues();

		values.put(COLUMN_NAME, "Rafael");
		values.put(COLUMN_CANCEL, false);
		db.insert(TABLE_NAME, null, values);

		values.clear();
		values.put(COLUMN_NAME, "Luisa");
		values.put(COLUMN_CANCEL, false);
		db.insert(TABLE_NAME, null, values);

		values.clear();
		values.put(COLUMN_NAME, "Sunção");
		values.put(COLUMN_CANCEL, false);
		db.insert(TABLE_NAME, null, values);

		values.clear();
		values.put(COLUMN_NAME, "Martinha");
		values.put(COLUMN_CANCEL, false);
		db.insert(TABLE_NAME, null, values);
		 */

	}

	public long insert(Match match) {

		ContentValues values = matchToContentValues(match);

		return inserir(TABLE_NAME, values);
	}

	public static ContentValues matchToContentValues(Match match) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_GOAL, match.getGoal());
		values.put(COLUMN_TOTAL, match.getTotal());
		values.put(COLUMN_ID, match.getId());
		values.put(COLUMN_OPEN, Utils.booleanToInteger(match.isOpen()));
		return values;
	}

	public boolean remove(long id) {
		return remover(TABLE_NAME, COLUMN_ID, id);
	}

	public Cursor searchAll() {
		return super.consultaBasica(TABLE_NAME, COLUMNS, null);
	}

	public Match searchMatch(long id) throws SQLException {
		Match match = null;
		Cursor cursor = consultar(TABLE_NAME, null, COLUMN_ID,String.valueOf(id));

		if (cursor!=null && cursor.getCount()>0){
			match = new Match();
			match.setGoal(cursor.getInt(cursor.getColumnIndex(MatchDAO.COLUMN_GOAL)));
			match.setTotal(cursor.getInt(cursor.getColumnIndex(MatchDAO.COLUMN_TOTAL)));
			match.setOpen(Utils.integerToBoolean(cursor.getInt(cursor.getColumnIndex(MatchDAO.COLUMN_GOAL))));
			match.setId(cursor.getLong(cursor.getColumnIndex(MatchDAO.COLUMN_ID)));
		}
		return match;
	}

	public long update(Match match) {
		ContentValues values = matchToContentValues(match);
		return atualizar(TABLE_NAME, values, new String[] { COLUMN_ID },new String[] { String.valueOf(match.getId()) });
	}

}
